
import java.sql.*;

public class App {
    public static void main(String[] args) throws Exception {
        App app = new App();
        app.createDB("test.db");
        //app.createTable();
        //app.insert();
        app.select();
        app.close();
    }
    Connection connection = null;
    void createDB(String name)throws Exception
    {
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite:"+name);
    }
    void close()throws Exception
    {
        connection.close();
    }
    void createTable()throws Exception{
        Statement statement = connection.createStatement();
        String sql = "CREATE TABLE STUDANT ("+
            "ID INTEGER PRIMARY KEY AUTOINCREMENT,"+
            "NAME TEXT NOT NULL,"+
            "AGE INT NOT NULL);";
        statement.executeUpdate(sql);
        statement.close();
    }
    void insert()throws Exception{
        Statement statement = connection.createStatement();
        String sql = "INSERT INTO STUDANT (NAME,AGE) "+
        "VALUES ('张三', 32);"; 
        statement.executeUpdate(sql);
        statement.close();
    }
    void update()throws Exception{
        Statement statement = connection.createStatement();
        String sql = "UPDATE STUDANT set NAME = '王五' where ID=1;";
        statement.executeUpdate(sql);
        statement.close();
    }
    void delete()throws Exception{
        Statement statement = connection.createStatement();
        String sql = "DELETE from STUDANT where ID=2;";
        statement.executeUpdate(sql);
        statement.close();
    }
    void select()throws Exception{
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery( "SELECT * FROM STUDANT;" );
        while ( rs.next() ) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            int age = rs.getInt("age");
            System.out.println( "ID = " + id );
            System.out.println( "NAME = " + name );
            System.out.println( "AGE = " + age );
            System.out.println();
        }
        rs.close();
        statement.close();
    }
}
